package com.warm.pump.module.sys.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.module.sys.bean.po.gen.Area;
import com.warm.pump.module.sys.service.IAreaService;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * sys_area
 */
@Controller
@RequestMapping("/area")
public class AreaController {
    @Autowired
    private IAreaService areaService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("area:list")
    public OutJson list(Integer page, Integer limit, Area area) {
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, area);

        if (StringUtils.isNotEmpty(area.getSidx()) && StringUtils.isNotEmpty(area.getOrder())) {
            map.put("orderBy", area.getSidx() + " " + area.getOrder());
        }
        //查询列表数据
        PageBean pageBean = areaService.getPageBeanByMap(map, page, limit);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("area:info")
    public OutJson info(@PathVariable("id") Integer id) {
        Area area = areaService.queryObject(id);

        return OutJson.ok().put("area", area);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("area:save")
    public OutJson save(@RequestBody Area area) {
        areaService.save(area);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("area:update")
    public OutJson update(@RequestBody Area area) {
        areaService.update(area);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("area:delete")
    public OutJson delete(@RequestBody Integer[] ids) {
        areaService.deleteBatch(ids);

        return OutJson.ok();
    }

}
